package com.cxhome.cxinfox.cxmenu;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.system.Menu;
import com.traffic.managent.model.system.Module;
import com.traffic.managent.model.system.Operator;
import com.traffic.managent.model.system.User;
import com.traffic.managent.util.SQLUtils;

/**
 * 数据字典service
 * 
 */
public class MenuService extends BaseService {

	public static final MenuService service = new MenuService();

	public Page<Menu> paginate(Menu model, Paginator paginate, StringBuilder orderSql) {

		SQLUtils sql = new SQLUtils(" from cx_menu t "
				+ " left join cx_menu p on t.parentmenuids = p.ids  left join cx_systems s on t.systemsids = s.ids ");

		// 查询条件
		if (model.getAttrValues().length != 0) {
			sql.setAlias("t");
			sql.append(" where 1=1 ");
			sql.whereLike("names_zhcn", model.getStr("names_zhcn"));
		}
		sql.append(orderSql);

		return Menu.dao.paginate(paginate, "select t.*,p.names_zhcn as parentName,s.names as sysnames", sql.toString());

	}

	public void saveEdit(Menu model) {
		Menu parentmodel = MenuService.service.getMenuByPdeptids(model.getStr("parentdepartmentids"));
		String pathName = parentmodel.getStr("pathsName");
		String paths = parentmodel.getStr("paths");
		pathName += "/" + model.getStr("names");
		paths += "/" + model.getStr("ids");
		model.put("pathsName", pathName);
		model.put("paths", paths);
		model.update();

	}

	/**
	 * 根据parentdepartmentids 查询Menu
	 */
	public Menu getMenuByPdeptids(String parentdepartmentids) {

		return Menu.dao.findFirst("select * from cx_department where 1=1 and ids = ?", parentdepartmentids);

	}

	public void operator_save(String menuids, String operatorids) {
		Db.update("update cx_menu set operatorids = ? where 1=1 and ids = ?", operatorids, menuids);
	}

	/**
	 * 获取根目录下拉框
	 * 
	 * @param selected
	 * @return
	 */
	public String selectMenu(Integer selected) {
		List<Menu> list = Menu.dao.findByWhere(" where status = 1 and parentid = 0 order by sort ");
		StringBuffer sb = new StringBuffer();
		for (Menu menu : list) {
			sb.append("<option value=\"");
			sb.append(menu.getInt("id"));
			sb.append("\" ");
			if (selected != null) {
				sb.append(menu.getInt("id") == selected ? "selected" : "");
			}
			sb.append(">");
			sb.append(menu.getStr("name"));
			sb.append("</option>");
		}
		return sb.toString();
	}

	/**
	 * 获取父节点名称
	 * 
	 * @param model
	 * @return
	 */
	public String getParentName(Menu model) {
		Integer parentid = model.getInt("parentid");
		if (parentid == null || parentid == 0) {
			return "根目录";
		}
		String parentName = Menu.dao.findById(model.getInt("parentid")).getStr("name");
		return parentName;
	}

	/**
	 * 获取父模块列表
	 * 
	 * @param parentid
	 * @return
	 */
	public List<Module> getParentMoudles() {
		List<Module> list = Module.dao.findByWhere(" where 1 = 1 and isparent = 'true' order by orderids ");
		return list;
	}

	/**
	 * 获取模块目录下的功能目录
	 * 
	 * @param parentid
	 * @return
	 */
	public List<Operator> getOperatorByModuleids(String modulesIds) {
		List<Operator> list = Operator.dao.findByWhere(" where 1 = 1 and moduleids = ? ", modulesIds);
		return list;
	}

	/**
	 * 获取菜单目录
	 * 
	 * @param parentid
	 * @return
	 */
	public List<Menu> getMenuListByParentid() {
		List<Menu> list = Menu.dao.findByWhere(" where 1 = 1 and isparent = 'true'  order by orderids ");
		return list;
	}

	/**
	 * 获取菜单目录
	 * 
	 * @param parentid
	 * @return
	 */
	public List<Menu> getMenuListByParentid(User user) {
		String sql = "select ids, names_zhcn, isparent, images from cx_menu where parentMenuIds = '8a40c0353fa828a6013fa898d4ac0020' order by orderIds asc ";
		List<Menu> listmenu = Menu.dao.find(sql);
		return listmenu;
	}
	// public List<CxMenu> getMenuListByParentid(CxUser user) {
	// /**
	// * 1,通过用于id获取roleIds 2,通过roleids 获取operatorIds 3,通过operatorids 获取
	// * menuList
	// */
	//
	// // 根据userid 获取roleids
	// String roleids = user.getStr("roleids");
	// String[] roleidsArr = roleids.split(",");
	// String roleidsstr = "";
	// for (int i = 0; i < roleidsArr.length; i++) {
	// roleidsstr += "'" + roleidsArr[i] + "',";
	// }
	// roleidsstr = roleidsstr.substring(0, roleidsstr.length() - 1);
	// // 查询角色
	// List<CxRole> listrole = CxRole.dao.find("select * from cx_role where 1=1
	// and ids in (" + roleidsstr + ")");
	//
	// // 查询角色拥有的菜单operatorids
	// String useroperatorids = "";
	// String useroperArrStr = "";
	// for (int i = 0; i < listrole.size(); i++) {
	// useroperatorids += listrole.get(i).getStr("operatorids");
	// }
	//
	// if (useroperatorids != null && !"".equals(useroperatorids)) {
	// String[] useroperArr = useroperatorids.split(",");
	// for (int i = 0; i < useroperArr.length; i++) {
	// useroperArrStr += "'" + useroperArr[i] + "',";
	// }
	// useroperArrStr = useroperArrStr.substring(0, useroperArrStr.length() -
	// 1);
	// }
	// useroperArrStr = useroperArrStr.replaceAll("operator_", "");
	// String sql = "select * from cx_menu where 1=1 and isparent = 'true' and
	// operatorids in (" + useroperArrStr + ")";
	// List<CxMenu> listmenu = CxMenu.dao.find(sql);
	//
	// if(listmenu == null || listmenu.isEmpty()){
	// listmenu = CxMenu.dao.find(""
	// + "SELECT * FROM cx_menu c where 1=1 and c.isparent = 'true' and c .ids
	// in("
	// + "select b.parentmenuids from cx_menu b where 1=1 and operatorids in ("
	// + useroperArrStr + "))");
	// }
	//
	// return listmenu;
	// }

	/**
	 * 根据moduleids 获取systemids， 根据systemids获取所有menuids
	 * 
	 * @param moduleids
	 * @return
	 */
	public String getAllmenuids(String moduleids) {
		String sql = " select group_concat(ids) as menuids from cx_menu "
				+ "where systemsids = (select systemsids from cx_module where ids = ?)";
		Record record = Db.findFirst(sql, moduleids);
		String menuids = record.getStr("menuids");
		return menuids;
	}
}
